package com.stox.parser;

import java.util.StringTokenizer;

import org.springframework.stereotype.Component;

import com.stox.model.Bar;

@Component
public class BseBarParser {

	public Bar parse(String str) {
		Bar bar = new Bar();
		StringTokenizer t = new StringTokenizer(str, ",", false);
		try {
			bar.setScripId(Long.parseLong(t.nextToken()));
			t.nextToken(); //Name
			t.nextToken(); //Group
			if (!"Q".equals(t.nextToken()))  return null;
			bar.setOpen(Double.parseDouble(t.nextToken()));
			bar.setHigh(Double.parseDouble(t.nextToken()));
			bar.setLow(Double.parseDouble(t.nextToken()));
			bar.setClose(Double.parseDouble(t.nextToken()));
			t.nextToken();//bar.setLast(Double.parseDouble(t.nextToken()));
			t.nextToken();//bar.setPreviousClose(Double.parseDouble(t.nextToken()));
			t.nextToken();//bar.setTrades(Double.parseDouble(t.nextToken()));
			bar.setVolume(Double.parseDouble(t.nextToken()));
			t.nextToken();//bar.setTurnover(Double.parseDouble(t.nextToken()));
		} catch (NumberFormatException nfe) {
			bar = null;
		} catch (Exception e) {
			e.printStackTrace();
			bar = null;
		}
		return bar;
	}

}
